package com.xiaoxiangxueyuan.chapters02;

import com.hardy.node.LinkedNode;

public class InversionLinkNode {

    public static void main(String[] args) {
        LinkedNode head = new LinkedNode(1);
        head.next = new LinkedNode(2);
        head.next.next = new LinkedNode(3);
        head.next.next.next = new LinkedNode(4);
        head.next.next.next.next = new LinkedNode(5);
        LinkedNode next = head;
        while (next!=null){
            System.out.print(next.value);
            next = next.next;
        }
        System.out.println();
        InversionLinkNode reverseLinkedList = new InversionLinkNode();
        head = reverseLinkedList.reverse1(head);
        LinkedNode next1 = head;
        while (next1!=null){
            System.out.print(next1.value);
            next1 = next1.next;
        }
        System.out.println();
        head = reverseLinkedList.reverse2(head);
        next1 = head;
        while (next1!=null){
            System.out.print(next1.value);
            next1 = next1.next;
        }
    }


    public LinkedNode reverse1(LinkedNode head){
        LinkedNode next = head;
        LinkedNode newHead = null;
        while (next!=null){
            next = head.next;
            head.next = newHead;
            newHead = head;
            head = next;
        }
        return newHead;
    }

    public LinkedNode reverse2(LinkedNode head){
        if(head==null||head.next==null){
            return head;
        }
        LinkedNode newHead = reverse2(head.next);
        head.next.next=head;
        head.next=null;
        return newHead;

    }
}
